<?php
class cache{
    var $cache_dir=MVMMALL_CACHE ;
    var $db;
    var $tablepre;
    function cache($db,$tablepre){
        $this->db       = $db;
        $this->tablepre = $tablepre;
    }
    //读取缓存
    function get_cache($cache_id)
    {
        if (file_exists($this->cache_dir.$cache_id.'.cache.php')) {
        	 include $this->cache_dir.$cache_id.'.cache.php';
        }else {
           $this->put_cache($cache_id);
           return  $this->$cache_id();
        }
       return $$cache_id;
    }
     //写入缓存
    function put_cache($cache_id)
    {
        $arr = $this->$cache_id();
        writeover($this->cache_dir.$cache_id.'.cache.php','<?php $'.$cache_id.'='.var_export($arr,true).'; ?>');
    }
    
    //分类缓存
    function category()
    {
        $category   = array();
        $result     = $this->db->query("SELECT * FROM `{$this->tablepre}category` ORDER BY category_rank ");
        while($rt   = $this->db->fetch_array($result)){
            $category[]=$rt;
        }
        return $category ;
    }
    
    //系统配置文件
    function cfg(){
         $arr      = array();
        $query     = $this->db->query("SELECT * FROM `{$this->tablepre}config`");
        while ($rt = $this->db->fetch_array($query)){
            $arr['cf_name']         = str_replace("'","\'",$rt['cf_name']);
            $arr[$rt['cf_name']] = $rt['cf_value'];
        }
        return $arr;
    }
    
    //会员等级
    function grade(){
        $arr           = array();
        $result        = $this->db->query("SELECT group_name,group_id FROM `{$this->tablepre}grade_table` ORDER BY `degree`");
        while ($rt     = $this->db->fetch_array($result)){
            $arr[0]               = 'Guest';
            $arr[$rt['group_id']] = $rt['group_name'];
        }
        return $arr;
    }
    
    //导航缓存文件处理
    function nav(){
        $nav_array   = array();
        $rs	         = $this->db->query("SELECT * FROM `{$this->tablepre}nav` ORDER BY view");
        while($navdb = $this->db->fetch_array($rs)){
            $style_array    = explode("|",$navdb['style']);
            $style_array[1] && $navdb['title']="<b>".$navdb['title']."</b>";
            $style_array[2] && $navdb['title']="<i>".$navdb['title']."</i>";
            $style_array[3] && $navdb['title']="<u>".$navdb['title']."</u>";
            $style_array[0] && $navdb['title']="<font color=\"$style_array[0]\">".$navdb['title']."</font>";
            $navdb['target'] = $navdb['target'] ? "target=\"_blank\"" : "";
            if($navdb['nav_type']==1){
                $nav_link    = parse_url($navdb['link']);
                $nav_query   = explode("=",$nav_link['query']);
                $nav_name    = explode(".",$nav_link['path']);
                $navdb['link'] = $nav_link['path']&&$nav_query[0] ? GetBaseUrl($nav_name[0],$nav_query[1],$nav_query[0]): GetBaseUrl($nav_name[0]);
            }
            $nav_array[]=$navdb;
        }
        return $nav_array;
    }
    //风格缓存
    function style($type='red'){
        $styles_array   = array();
        $result         = $this->db->query("SELECT uid,name,available FROM `{$this->tablepre}styles` WHERE available='1'");
        while($skinfile = $this->db->fetch_array($result)){
            $skinfile['title']   = $skinfile['name'];
            $skinfile['url']     = "&style_id=$skinfile[uid]";
            $styles_array[]      = $skinfile;
        }
        return $styles_array;
    }
    //投票
    function poll($type='red'){
        $poll_array  = array();
        $poll_array  = $this->db->get_one("SELECT * FROM `{$this->tablepre}poll_table` ORDER BY end_date DESC LIMIT 0,1");
        return $poll_array;
    }
    
    //弹窗广告
    function poup(){
        $m_now_time = time();
        $result  = $this->db->query("SELECT * FROM `{$this->tablepre}popup_table` WHERE start_date< '$m_now_time' AND end_date > '$m_now_time' ORDER BY `uid` DESC");
        while($rt = $this->db->fetch_array($result)) {
            $popup_list.="popupWin=window.open(\"popup.php?uid=$rt[uid]\",\"$rt[uid]\",\"left=$rt[popup_left],top=$rtt[popup_top],width=$rt[popup_width],height=$rt[popup_height],toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,alwaysRadised=0\");";
            $popup_list.="popupWin.opener.name=\"mvmmall\";";
        }
        return $popup_list;
    }
    //所有的分类树
    function tree(){
        require_once MVMMALL_ROOT.'./include/category_tree.class.php';
        $tree = new tree();
        //查询数据库，返回分类的ＩＤ，名称，父类３个字段
        $result = $this->db->query("SELECT uid,category_name,category_id,category_rank,use_direct FROM `{$this->tablepre}category`  ORDER BY category_id,category_rank ASC");
        //遍历结果集，并压入无限分类
        while ($rt = $this->db->fetch_array($result)){
        	$rt['use_direct'] = $rt['use_direct']==1 ? $GLOBALS['lang']['yes'] : $GLOBALS['lang']['no'];
            $tree->new_node($rt['uid'],$rt['category_name'],$rt['category_id'],$rt['use_direct'],$rt['category_rank']);
        }
        $GLOBALS['cache']->put_cache('right_tree');
        return $tree->get_childs() ;
    }
    
    //右边分类
    function right_tree(){
        $arr   = array();
        $query = $this->db->query("SELECT uid,category_id,use_direct,category_rank,category_name,category_file1 FROM `{$this->tablepre}category` WHERE category_id = '0' AND use_direct = 1  ORDER BY category_rank");
        while($rt       = $this->db->fetch_array($query)){
            $sc_result  = $this->db->query("SELECT uid,use_direct,category_rank,category_name,category_file1 FROM `{$this->tablepre}category` WHERE category_id='$rt[uid]' AND use_direct = 1  ORDER BY category_rank");
            while($rs   = $this->db->fetch_array($sc_result)) {
                $rs['url']     = $GLOBALS['rewrite'] == '1' ? "category-$rs[uid]-1.html":"category.php?action=$rs[uid]";
                $rs['title']   = $rs['category_file1'] ? "<img src='$rs[category_file1]' border='0' alt='$rs[category_name]'> ":''.$rs['category_name'];
                $category[]    = $rs;
            }
            $rt['children']    = $category;
            $rt['url']         = $GLOBALS['rewrite'] == '1' ? "category-$rt[uid]-1.html":"category.php?action=$rt[uid]";
            $rt['title']       = $rt['category_file1'] ? " <img src='$rt[category_file1]' border='0' alt='$rs[category_name]'> " : ''.$rt['category_name'];
            $arr[]             = $rt;
            unset($category);
        }
        return $arr;
    }
    //品牌
    function brand(){
        unset($mm_brand);
        $mm_brand   = array();
        $result     = $this->db->query("SELECT id,brandname FROM `{$this->tablepre}brand_table` ORDER BY `train` ");
        while($rt   = $this->db->fetch_array($result)){
             $mm_brand[0]        = '--';
            $mm_brand[$rt['id']] = $rt['brandname'];
        }
        return $mm_brand;
    }
    
     //产品包装
     function pack()
     {
         $pack     = array();
         $result   = $this->db->query("SELECT * FROM `{$this->tablepre}pack` ");
         while($rt = $this->db->fetch_array($result)){
             $rt['fee']   = currency($rt['fee']);
             $rt['desc']  = $GLOBALS['lang']['fee'].','.$rt['fee'].','.$rt['pack_desc'];
             $rt['price'] = currency($rt['price']);
             $rt['photo'] && $rt['photo'] = "<a href='$rt[photo]' id='pack_photo' target=\"_blank\"><img src=\"images/admincp/pack.gif\" border=\"0\"></a>";
              $pack[]     = $rt;
         }
         return $pack;
     }
     
     //公告牌列表缓存
     function board(){  
         $board_arr = array();
         $result    = $this->db->query("SELECT board_title,board_name_code FROM `{$this->tablepre}badmin_table` ORDER BY uid DESC");
         while ($rt = $this->db->fetch_array($result)){
             $rt['url']     = GetBaseUrl('board',$rt['board_name_code']);
             $rt['title']   = $rt['board_title'];
             $board_arr[]   = $rt;
         }
         return $board_arr;
     }
     //推广图片
     function info(){
         $info_array = array();
         $info_array    = $this->db->get_all("SELECT * FROM `{$this->tablepre}cycle` WHERE type_img='1'");
         return $info_array;
     }
     //配送列表
     function shipping()
     {
         $ship         = array();
         $result       = $this->db->query("SELECT name,id FROM `{$this->tablepre}ship_table`");
         while ( $rt   = $this->db->fetch_array($result)) {
             $ship[$rt['id']] = $rt['name'];
         }
         return $ship;
     }
     //友情连接
     function links(){
         $friend_links = array();
         $result   = $this->db->query("SELECT * FROM `{$this->tablepre}forumlinks_table` ORDER BY `displayorder` ");
         while($rt = $this->db->fetch_array($result)){
             $rt['title']        = $rt['name'];
             $friend_links[]     = $rt;
         }
         return $friend_links;
     }
     //公告牌分类
     function board_cat()
     {
     	$board_array = array();
        $result      = $this->db->query("SELECT ps_name,uid,category_name FROM `{$this->tablepre}bcategory`");
        while($rt    = $this->db->fetch_array($result)) {
        	$board_array[] = $rt;
        }
        return $board_array;
     }
     //管理菜单
     function menu_tree(){
     	 require_once MVMMALL_ROOT.'./include/category_tree.class.php';
        $tree = new tree();
     	$result = $this->db->query("SELECT * FROM `{$this->tablepre}admin_menu`  ORDER BY menu_id,menu_order ASC");
     	//遍历结果集，并压入无限分类
     	while ($rt = $this->db->fetch_array($result)){
     		$rt['use_home'] = $rt['use_home']==1 ? $GLOBALS['lang']['yes'] : $GLOBALS['lang']['no'];
     		$tree->new_node($rt['uid'],$rt['menu_name'],$rt['menu_id'],$rt['use_home'],$rt['menu_order']);
     	}
     	return $tree->get_childs() ;
     }
}